package com.doit;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.List;

/**
 * @DATE 2021/12/10/17:36
 * @Author MDK
 * @Version 2021.2.2
 *  子节点个数变化
 *
 **/
public class C06_Watcher3 {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
        List<String> children = zk.getChildren("/", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    System.out.println("/下的子节点个数变化"+watchedEvent.getType());
                    //重新获取列表
                    List<String> children1 = zk.getChildren("/", this);
                    System.out.println("更新下的节点是:"+children1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println("当前/下所有的节点是" +children);
        Thread.sleep(Integer.MAX_VALUE);
    }
}
